﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Description;
using System.Text;
using System.Threading.Tasks;

namespace SelfHostedBookingService
{
    class Program
    {
        //Selfhosting: http://msdn.microsoft.com/en-us/library/ms731758.aspx
        static void Main(string[] args)
        {
            String dnsHost = "benjamin-PC";
            Int32 dnsPort = 88;

            ServiceHost svcHost = new ServiceHost(typeof(ServiceReference1.InternalBookingServiceClient), new Uri("http://" + dnsHost + ":" + dnsPort + "/InternalBookingService"));
            try
            {
                // Check to see if the service host already has a ServiceMetadataBehavior
                ServiceMetadataBehavior smb = svcHost.Description.Behaviors.Find<ServiceMetadataBehavior>();
                // If not, add one
                if (smb == null)
                    smb = new ServiceMetadataBehavior();
                smb.HttpGetEnabled = true; //Allow HTTP GET access!

                smb.MetadataExporter.PolicyVersion = PolicyVersion.Policy15;
                svcHost.Description.Behaviors.Add(smb); //Add the behaviour
                // Add MEX endpoint
                svcHost.AddServiceEndpoint(
                  ServiceMetadataBehavior.MexContractName,
                  MetadataExchangeBindings.CreateMexHttpBinding(),
                  "mex"
                );
                // Add application endpoint
                BasicHttpBinding basicHttpBinding = new BasicHttpBinding();
                basicHttpBinding.Security.Mode = BasicHttpSecurityMode.None;
                svcHost.AddServiceEndpoint(typeof(ServiceReference1.IInternalBookingService), basicHttpBinding, "http://" + dnsHost + ":" + dnsPort);

                //svcHost.Description.Endpoints[0].Behaviors.Add(new httpbe { HelpEnabled = true });           

                // Open the service host to accept incoming calls
                svcHost.Open();

                // The service can now be accessed.
                Console.WriteLine("The service is running!");
                Console.WriteLine("Press <ENTER> to terminate service.");
                Console.WriteLine();
                Console.ReadLine();

                // Close the ServiceHostBase to shutdown the service.
                svcHost.Close();
            }
            catch (CommunicationException commProblem)
            {
                Console.WriteLine("There was a communication problem. " + commProblem.Message);
                Console.Read();
            }
        }
    }
}
